package com.cs.base.util;

import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import java.lang.ref.WeakReference;

/**
 * @ClassName: SafeHandler
 * @Date: 2021/11/9
 * @Author: CS
 * @Description:
 */
public class SafeHandler<T> extends Handler {
    WeakReference<T> weakReference;
    HandListener<T> listener;
    public SafeHandler(T t, HandListener<T> listener){
        weakReference = new WeakReference<T>(t);
        this.listener = listener;
    }
    @Override
    public void handleMessage(@NonNull Message msg) {
        T t=weakReference.get();
        listener.handleMessage(t,msg);
    }

    interface HandListener<T>{
        void handleMessage(T t,@NonNull Message msg);
    }
}
